/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DA;

/**
 *
 * @author thangvv0907i
 */
import java.sql.*;
/**
 *
 * @author hieubm0708l
 */
public class ConnectionDB {

    private static Connection conn;
    private static String servername;
    private static String port;
    private static String username;
    private static String password;
    private static String database;

    public static Connection getConnection() {
        return conn;
    }

    public static boolean connect(String _serverName, String _port, String _username, String _password, String _database) {
        servername = _serverName;
        port = _port;
        username = _username;
        password = _password;
        database = _database;

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(getConnectionString());
        } catch (Exception ex) {
            return false;
        }

        return true;
    }

    public static String getConnectionString() throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("jdbc:sqlserver://");
        sb.append(servername);
        sb.append(":");
        sb.append(port);

        sb.append(";databaseName=");
        sb.append(database);

        sb.append(";user=");
        sb.append(username);

        sb.append(";password=");
        sb.append(password);

        return sb.toString();
    }

    public static void exec(String sql) {
        try {
            Statement query = getConnection().createStatement();
            query.execute(sql);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }

    }

    public static String getDatabase() {
        return database;
    }

    public static String getPassword() {
        return password;
    }

    public static String getPort() {
        return port;
    }

    public static String getServername() {
        return servername;
    }

    public static String getUsername() {
        return username;
    }


}

